XtGem Forum catalog
In Java, all variables must be declared before they can be used.

The basic form of avariable declaration is shown here:
type identifier [= value][, identifier [= value]...];
The type is one of Java's datatypes. The identifieris the name of the variable. To declare more than one variable of the specified type, use a comma-separated list.
Here are several examples of variable declarations of various types. Note that some include an initialization.


int a, b, c;// declares three ints, a, b, and c.int d =3, e, f =5;// declares three moreints, initializing// d and f.byte z =22;// initializesz.double pi =3.14159;//declares an approximation of pi.char x ='x';// the variable x has the value 'x'.

This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java:

1. Local variables
2. Instance variables
3. Class/static variables


class Sls
{
public static void main()
{
int s,l,s; /* This command shows the declaration of integer varialbe's named s, l and s.. *\

int m=5,n=3,o=9;/*This command is similar to previous one but the integer variables declared m,n and o stores integer values. The process of storing values in variables is termed as initialization.*/

char c='z'; /* c is a character type variable which is initialized with a character z.*\
}
}

Local Variables
Local variables are declaredin methods, constructors, orblocks.
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.


Access modifiers cannot be used for local variables.

Local variables are visible only within the declared method, constructor or block.

Local variables are implemented at stack level internally.

There is no default value for local variables so local variables should be declared and an initial valueshould be assigned before the first use.


Example ::
Here age is a local variable. This is defined insidepupAge()method and its scope is limited to this method only.
public class Sls
{
public void pupAge ()
{
int age = 0 ;
age = age 7 ; System . out . println ( "Puppy age is : " age );
}
public static void main ( String args [])
{Sls test = new Sls (); test . pupAge (); } }